package com.hw.one.dam2.dao;

import com.hw.one.dam2.param.AirParam;
import com.hw.one.dam2.po.MonitorBaseDataPo;
import com.hw.one.dam2.po.RegionAirQualityPO;
import com.hw.one.dam2.po.SiteAirQualityPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Copyright 2017 汉威.智慧环保事业部
 * Created by 毛超然 on 2017/6/9 0009.
 */

@Repository(value = "airDao")
public class AirDao implements IAirDao {

    @Autowired
    private MongoTemplate mongoTemplateStandardSiteData;

    @SuppressWarnings("rawtypes")
    @Override
    public List<RegionAirQualityPO> findAirDataByRegionCodesAndTime(AirParam airParam) {

        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("MONITORTIME").gte(airParam.getStartTime())
            .lte(airParam.getEndTime());
        criteria.and("REGIONCODE").in(airParam.getRegionCodes());

        // 创建查询对象
        Query query = new Query(criteria);
        // 增加排序功能
        query.with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));
        // 查询数据
        List<RegionAirQualityPO> airList = mongoTemplateStandardSiteData.find(query, RegionAirQualityPO.class, airParam.getTable());

        return airList;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public List<SiteAirQualityPO> findAirDataBySiteCodesAndTime(AirParam airParam) {
        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("MONITORTIME").gte(airParam.getStartTime())
            .lte(airParam.getEndTime());
        criteria.and("SITECODE").in(airParam.getSiteCodes());

        // 创建查询对象
        Query query = new Query(criteria);
        // 增加排序功能
        query.with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));
        // 查询数据
        List<SiteAirQualityPO> airList = mongoTemplateStandardSiteData.find(query, SiteAirQualityPO.class, airParam.getTable());

        return airList;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public List<MonitorBaseDataPo> findAirDataByTime(AirParam airParam){
        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("MONITORTIME").gte(airParam.getStartTime())
            .lte(airParam.getEndTime());
        criteria.and("SNCODE").in(airParam.getSiteCodes());
        // 创建查询对象
        Query query = new Query(criteria);
        // 增加排序功能
        query.with(new Sort(Sort.Direction.DESC, "MONITORTIME"));
        // 查询数据
        List<MonitorBaseDataPo> airList = mongoTemplateStandardSiteData.find(query, MonitorBaseDataPo.class, airParam.getTable());

        return airList;
    }
}
